package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.domain.Login;
import cn.gyx.hrm.mapper.LoginMapper;
import cn.gyx.hrm.service.ILoginService;
import cn.gyx.hrm.service.IMealService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author gyx
 * @since 2021-11-22
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private IMealService mealService;

    @Override
    @Transactional
    public Login saveLoginAndMealID(Login login, Long mealId) {
       if (StringUtils.isEmpty(login.getUsername())){
           throw new RuntimeException("用户名不能为空");
       }
        if (StringUtils.isEmpty(login.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        if (mealId == null){
            throw new RuntimeException("套餐id不能为空");
        }

        // 保存login
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setCredentialsNonExpired(true);
        login.setAccountNonLocked(true);
        insert(login);

        // 保存套餐
        Long loginId = login.getId();// 获取id

        // 保存套餐方法
        mealService.saveCombo(mealId,loginId);

        return login;
    }
}
